package com.eliza.base

import com.eliza.library.utils.net.OptionResult
import com.eliza.library.utils.net.PageInfo
import com.eliza.library.utils.net.RequestID
import com.eliza.library.utils.net.RespResult
import com.eliza.library.utils.net.HttpClient
import com.eliza.library.utils.net.BaseServer
import io.ktor.client.request.headers
import io.ktor.client.request.setBody
import io.ktor.http.HttpMethod
import io.ktor.http.ParametersBuilder
import io.ktor.http.URLBuilder
import io.ktor.http.path
import io.ktor.server.application.call
import io.ktor.server.response.respond
import io.ktor.server.routing.get
import kotlinx.coroutines.runBlocking
import org.junit.Test

class ExampleHttpClient {
    @Test
    fun ktv() {
        runBlocking {
            var urlBuilder =
                URLBuilder("http://admin.ktv2t7iv2gxichec.com")
            val baseClient = HttpClient(urlBuilder)
            val resp = baseClient.post("/api/v1/media/topic/getById") {
                this.headers {
                    set(
                        "UserAuth",
                        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxUGFuZWwiLCJleHAiOjE3MTU2Nzc0NDUsImlhdCI6MTcxNDM4MTQ0NSwiaWQiOjAsImRhdGEiOnsiaWQiOjEsInVzZXJuYW1lIjoiMDgyMjMwNzEwMGJjIiwicGFzc3dvcmQiOiJlZSIsImVtYWlsIjoiIiwicGhvbmUiOiIiLCJhY3RpdmUiOjEsInN0YXR1cyI6MCwiZXhwX3RpbWUiOiIyMDI1LTA0LTE5VDE2OjI2OjEzKzA4OjAwIiwiZGV2aWNlIjp7InVzZXJuYW1lIjoiMDgyMjMwNzEwMGJjIiwicGFzc3dvcmQiOiJlZSIsImNvdW50cnkiOiJDTiIsInZlcnNpb24iOjEsInZlcnNpb25fbmFtZSI6IjEuMC4xIiwidGltZXN0YW1wIjoxNzE0MzgwNzQ0LCJ3aWZpX21hYyI6IjVjYzJmMTVmY2QwMCIsImxhZyI6InpoIiwibWFjIjoiMDgyMjMwNzEwMGJjIiwibWQ1IjoiNWJlOTc2N2YyY2ZkZjllY2YwMzk3MzY4MjIwOWZlZDEiLCJhcHBfaWQiOiI1NzY3IiwiYXBwX3RrIjoiMzMyN0ExQzM4QkQxMzE0OTAzQjI1RjNFMTc5QzQxMkUiLCJkZXZpY2UiOiJyYXZlbiIsImFwcGxpY2F0aW9uX2lkIjoiY29tLmFuZHJvaWQua3R2Ym94LnR2IiwiYW5kcm9pZF9zZGtfdmVyc2lvbiI6MzEsImhfaWQiOiJTUDFBLjIxMTEwNS4wMDQiLCJmaXJtd2FyZV9kYXRlIjoiMjAyMy0xMS0yMyAxNzoyMjozNCIsImhfYm9hcnJkIjoiZXhkcm9pZCIsImhfYnJhbmQiOiJnb29nbGUiLCJoX2NwdV9hYmkiOiJhcm1lYWJpLXY3YSIsImhfY3B1X2FiaV8yIjoiYXJtZWFiaSIsImhfaGFyZHdhcmUiOiJzdW41MGl3OXAxIiwiaF9tb2RlbCI6Ik0zIiwiaF9zZXJpYWwiOiJDU0g2MThMR1g4MjgyMzAxODgiLCJtX2tleSI6IjdFNjk0MjQ1Q0I5QzFBRDEzMUE1Q0I3OUQ0MTQxQjNGIn0sImNvbWJvX2lkIjoxLCJkZXZpY2Vfc2VyaWFsIjoiQ1NINjE4TEdYODI4MjMwMTg4IiwiZGV2aWNlX2hhcmR3YXJlIjoic3VuNTBpdzlwMSIsImRldmljZV9tb2RlbCI6Ik0zIiwibWFjIjoiMDgyMjMwNzEwMGJjIiwiY29tYm8iOnsiaWQiOjEsIm5hbWUiOiJCbG9jayBUVyIsImxpbWl0X2FyZWFfb3BlcmF0aW9uIjoyLCJkZXNjcmlwdGlvbiI6IuWxj-iUvSBjbiB0dyIsImlzX2xpbWl0IjowLCJsaW1pdF9hcmVhcyI6WyJUVyIsIk5QIiwiQU8iLCJBRiIsIkFYIl0sImxpbWl0X3NwZWVkIjoxLCJ0b3BpY19pZHMiOls4LDEsMiwzLDQsNiw1XX19fQ.3lYR480aGBgRnGIGTT9KyZQGPPN3HjkqTxGe7EhF1io"
                    )
                    set("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                    set("Content-Type", "application/json")
                    set("Accept", "*/*")
                    set("Host", "admin.ktv2t7iv2gxichec.com")
                    set("Connection", "keep-alive")
                }.build()
                this.setBody(RequestID(id = 2))
            }
            println("=-=========/ =====")
//            println(resp.bodyAsText())

        }
    }

    @Test
    fun get() {
        runBlocking {
            var urlBuilder = URLBuilder(
                host = "192.168.20.4",
                port = 8081
            )
            val baseClient = HttpClient(urlBuilder)
            val get = baseClient.getx<RespResult<PageInfo>>("/") {}
            println("=-==============")
            when (get) {
                is OptionResult.Success -> println(get.data)
                is OptionResult.Error -> println(get.exception)
            }

//            val get = baseClient.get<ResultResp<Map<String, Int>>>("http://192.168.8.20:9999/api/v1/media/base/info") {}
            val asd = baseClient.postx<RespResult<PageInfo>>("/a") {}
            println("=-=========/ =====")
            when (asd) {
                is OptionResult.Success -> println(asd.data)
                is OptionResult.Error -> println(asd.exception)
            }
        }
    }

    @Test
    fun addisdfct() {
        var urlBuilder = URLBuilder(
            host = "192.168.8.20",
            port = 8080, pathSegments = listOf("aa", "bb"),
            parameters = ParametersBuilder().apply {
                append("ok", "12")
            }.build()
        )
        urlBuilder = URLBuilder(
            host = "192.168.8.20",
            port = 8080, pathSegments = listOf("aa", "bb")
        )
        println(urlBuilder)
        urlBuilder.path("sa")
        println(urlBuilder)
    }
}

class ExampleHttpServer {
    @Test
    fun get() {
        runBlocking {
            val baseServer = BaseServer(8080)
            baseServer.start {
                baseServer.setRoute("as", HttpMethod.Get) {
                    this.get {
                        this.call.respond("asd")
                    }
                }(this)
                baseServer.setRouting {
                    get("/") {
                        this.call.respond("asad")
                    }
                }(this)
            }
        }
    }

}